import Vue from 'vue'
import Vuex from 'vuex'
import jiankang from './jiankang'
import persistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  // 类比于 data
  state: {
    food: '西兰花炒蛋',
    name: '康哥',
    age: 28,
    male: true,
    friend: ['铁甲小宝', '宫本武藏', '亚索'],
    sports: '跑步',
    equip: '运动鞋 + 补水带',
    meat: '酱牛肉',
    count: 10
  },
  // 修改数据的方法
  mutations: {
    changeName (state, newName) {
      console.log('state:', state)
      // state.name = '食戬之灵'
      state.name = newName
    },
    changecount (state, m) {
      state.count += m
    },
    changesex (state) {
      state.male = !state.male
    },
    addcount (state, m) {
      state.count += m
    }
  },
  // 根据数据获取新的数据
  getters: {
    genderbos: state => {
      if (state.male === true) {
        return '帅哥'
      } else {
        return '美女'
      }
    }
  },
  actions: {
    addnum (context) {
      setTimeout(() => {
        context.commit('addcount', 5)
      }, 1000)
    },
    delnum (context) {
      setTimeout(() => {
        context.commit('addcount', -5)
      }, 1000)
    }
  },
  modules: {
    jiankang
  },
  plugins: [
    persistedState({
      storage: window.sessionStorage,
      paths: ['name', 'jiankang.msg', 'jiankang.msg2']
    })
  ]
})
